﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace EasyNet.Componets.Core.Utils
{
    public static class AssemblyUtil
    {
        #region 加载文件
        /// <summary>
        /// 加载文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static Assembly LoadFile(string file)
        {
            return Assembly.Load(File.ReadAllBytes(file));
        }
        #endregion

        #region 获取程序集Type
        /// <summary>
        /// 获取程序集所有Type
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static List<Type> GetTypeList(string file)
        {
            var assembly = LoadFile(file);
            return assembly.GetTypes().ToList();
        }
        /// <summary>
        /// 获取程序集制定Type列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="file"></param>
        /// <returns></returns>
        public static List<Type> GetTypeList<T>(string file)
        {
            var assembly = LoadFile(file);
            var tempType = typeof(T);
            return assembly.GetTypes().Where(w => tempType.IsAssignableFrom(w)).ToList();
        }
        /// <summary>
        /// 获取程序集制定Type
        /// </summary>
        /// <param name="file"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public static Type GetType(string file, string typeName)
        {
            var assembly = LoadFile(file);
            return assembly.GetTypes().FirstOrDefault(w => w.Name == typeName || w.FullName == typeName);
        }
        #endregion

        #region 获取程序集类型名称
        /// <summary>
        /// 获取制定Type名称集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="file"></param>
        /// <returns></returns>
        public static List<string> GetTypeName<T>(string file)
        {
            var assembly = LoadFile(file);
            var tempType = typeof(T);
            return assembly.GetTypes().Where(w => tempType.IsAssignableFrom(w))
                 .Select(s => s.Name).ToList();
        }
        /// <summary>
        /// 获取制定Type全名集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="file"></param>
        /// <returns></returns>
        public static List<string> GetTypeFullName<T>(string file)
        {
            var assembly = LoadFile(file);
            var tempType = typeof(T);
            return assembly.GetTypes().Where(w => tempType.IsAssignableFrom(w))
                 .Select(s => s.FullName).ToList();
        }
        /// <summary>
        /// 获取程序集中的类名称
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static List<string> GetPublicClassName(string file)
        {
            var assembly = LoadFile(file);
            return assembly.GetTypes().Where(w => w.IsClass && w.IsPublic).Select(s => s.Name).ToList();
        }

        /// <summary>
        /// 获取程序集中的类名称
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static List<string> GetPublicClassFullName(string file)
        {
            var assembly = LoadFile(file);
            return assembly.GetTypes().Where(w => w.IsClass && w.IsPublic).Select(s => s.FullName).ToList();
        }
        #endregion

    }
}
